package com.thoughtworks.shoutbox.client.shouts;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.thoughtworks.shoutbox.client.room.RoomEntranceListener;

public class ShoutsView extends Composite implements ShoutListener {
	private ShoutListView shoutList;
	private String currentRoom;
	private Label roomNameHeading;
	private RoomEntranceListener roomEntranceListener;
	private ShoutCreationView shoutCreation;

	public ShoutsView() {
		FlowPanel rootPanel = new FlowPanel();
		shoutList = new ShoutListView();
		shoutCreation = new ShoutCreationView(this);

		roomNameHeading = new Label();
		rootPanel.add(roomNameHeading);
		rootPanel.add(shoutList);
		rootPanel.add(shoutCreation);

		Anchor backToRoomListing = new Anchor("<< Back to Room Selection");
		backToRoomListing.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				roomEntranceListener.exitRoom(currentRoom);
			}
		});
		rootPanel.add(backToRoomListing);
		initWidget(rootPanel);
	}

	public void showShoutsOf(String roomId, String roomName) {
		this.currentRoom = roomName;
		shoutList.setRoomId(roomId);
		shoutCreation.setRoomId(roomId);
		
		roomNameHeading.setText("You are in room: " + roomName);
		shoutList.reloadShouts();
	}

	public void onShout() {
		shoutList.reloadShouts();
	}

	public void setRoomListener(RoomEntranceListener roomEntranceListener) {
		this.roomEntranceListener = roomEntranceListener;
	}
}
